var VHA_MOD_FILTER = (function(api) {

	var config, pvt ={};
	api.init = function (settings) {
		config = {
			classes: {
				pricingContainer: 	".pricing-filter-main",
				pricingFiltered: 	"pricing-card-filtered",
				pricingTiles: 	".pricing-card",
				filter: {
					container: ".vha-mod-plan-filter",
					radio: ".vha-vca-purple-radio li",
					options: ".vha-filter",
					question: ".question",
					on: "on"
				}
			}
		};
		pvt.setup();
	};

	pvt.setup = function() {
		$allQuestions = $(config.classes.filter.container).find(" div.question") ;
		$allQuestions.hide();
		pvt.bindFilter();
		pvt.filter.onload();
	};

	// Bind Filter: Bind the radio filter clicks
	pvt.bindFilter = function(){
		$(document.body).delegate(config.classes.filter.radio, "click", pvt.filter);
	};

	// Filter: Run the filter actions . Called via the filter radio's
	pvt.filter = function(){
		// This would be better if the rows had classnames
		var $input = $(this);
			$parent = $input.closest("div"),
			$container = $parent.closest(config.classes.filter.container),
			$nextQuestion = $("#" + $input.data("filter-id"));
			$otherQuestions = $container.find("div:gt(" + $parent.index() + ")").not($nextQuestion);
		// Add / Remove on states
		pvt.filter.setStates($input, $parent);
		// Check if there are any questions that need to be made visible
		$nextQuestion.addClass("on");
		// Find if there is a default question
		$otherQuestions.removeClass('on');
		$nextQuestion.find("[data-default='on']").trigger("click");
		
		//Shows the questions required
		pvt.filter.showQuestions($allQuestions);
		
		// Retrieve a list of ids from all the active radio buttons
		var ids = pvt.filter.buildID($container);
		// Show / Hide any tiles
		pvt.filter.tiles(ids);
	};
	
	pvt.filter.showQuestions = function(){
		$allQuestions.each(function(i,question){
			if($(question).hasClass('on')){
				$(question).show();
			}
			else
			$(question).hide();
			});
	}
	
	pvt.filter.onload = function(){
		var isFirst= $(config.classes.filter.options).first().find("li[data-default='on']");
		if (isFirst.length > 0)
			$(isFirst).trigger("click");
		else {
			pvt.filter.showQuestions($allQuestions);
			var ids = pvt.filter.buildID($(config.classes.filter.container));
			pvt.filter.tiles(ids);
		}
		
	};

	// Update Filter ID: Loop through the on filters and build a list of ids
	pvt.filter.buildID = function($container){

		var ids = "";

		// Loop through all active radio buttons
		$allQuestions.each(function(i,question){
		if($(question).hasClass('on')){
			$(question).find(config.classes.filter.radio + ".on").each(function(i, ele){
				ids += ".tile-filter-" + $(ele).attr("id"); 
			});
		}
		});
		return ids;
	};
	
	pvt.filter.setStates = function($input, $parent){
		// Set the on state on this and remove on others
		$parent.find(config.classes.filter.radio).not($input).removeClass(config.classes.filter.on);
		// Add the active class to the radio button
		$input.addClass(config.classes.filter.on);
	};

	// Filter Tiles: Filter/Unfilter any tiles that need to be visible
	pvt.filter.tiles = function(ids){
		var $showTiles = $(ids),
			$otherTiles = $(config.classes.pricingTiles).not($showTiles).addClass(config.classes.pricingFiltered);
		// Add class to other tiles
		$showTiles.removeClass(config.classes.pricingFiltered);
		$otherTiles.addClass(config.classes.pricingFiltered);

		// Re-center and setup margins for the carousel
		$.publish("/tile/rejig", [$(config.classes.pricingContainer)]);
	};

return api;

})(VHA_MOD_FILTER || {});


$(function() {
	VHA_MOD_FILTER.init();
});